package com.bysj.money_diary;

import java.util.ArrayList;
import java.util.List;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class LineChart extends Fragment {
	private View view;
	private LineChartView lineChart;
	private Data dataHelper;
	private int year,month;
	private int flag = 0;//用于区分支出和收入 0：支出      1：收入
	String[] date1 = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","日期"};//X坐标ע
	String[] date2 = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","日期"};
	String[] date3 = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","日期"};
	String[] date4 = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","日期"};
	int[] score= {0};//图表数据
	String tag = null;
	private List<PointValue> mPointValues = new ArrayList<PointValue>();
	private List<PointValue> mPointValues1 = new ArrayList<PointValue>();
	private List<AxisValue> mAxisXValues = new ArrayList<AxisValue>();
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.kantubiao_linechart, container, false);
		lineChart = (LineChartView)view.findViewById(R.id.line_chart);
		
		initView();
		getAxisXLables();//获取X轴坐标ע
        getAxisPoints();//获取坐标点
        initLineChart();//图表初始化
		return view;
	}
	private void initView() {
		
		year = GetTime.getYear();
		month = GetTime.getMonth();
//		year = ((Kantubiao) (LineChart.this.getParentFragment())).year;
//		month = ((Kantubiao) (LineChart.this.getParentFragment())).month;
		dataHelper = new Data(this.getActivity());
		Log.i(tag, "year = " + year);
		Log.i(tag, "month = " + month);
	}
	/**
     * 设置属性
     */
    private void initLineChart(){
        Line line = new Line(mPointValues).setColor(Color.parseColor("#f46635"));  //线的颜色
        Line line1 = new Line(mPointValues1).setColor(Color.parseColor("#73d271")); 
	    List<Line> lines = new ArrayList<Line>();    
	    line.setShape(ValueShape.CIRCLE);//折线图上每个数据点的形状  这里是圆形 （有三种 ：ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND）
	    line.setCubic(true);//曲线是否平滑，即是曲线还是折线
	    line.setStrokeWidth(3);
		line.setFilled(false);//是否填充曲线的面积
		line.setHasLabels(false);//曲线的数据坐标是否加上备注
		line.setHasLines(true);//是否用线显示。如果为false 则没有曲线只有点显示 
		line.setHasPoints(true);//是否显示圆点 如果为false 则没有原点只有点显示（每个数据点都是个大的圆点）
		lines.add(line);
		line1.setShape(ValueShape.DIAMOND);//折线图上每个数据点的形状  这里是圆形 （有三种 ：ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND）
	    line1.setCubic(true);//曲线是否平滑，即是曲线还是折线
	    line1.setStrokeWidth(3);
		line1.setFilled(false);//是否填充曲线的面积
		line1.setHasLabels(false);//曲线的数据坐标是否加上备注
		line1.setHasLines(true);//是否用线显示。如果为false 则没有曲线只有点显示 
		line1.setHasPoints(true);//是否显示圆点 如果为false 则没有原点只有点显示（每个数据点都是个大的圆点）
		lines.add(line1);
	    LineChartData data = new LineChartData();   
	    data.setLines(lines);   
	      
	    Axis axisX = new Axis(); 
	    axisX.setHasTiltedLabels(false);  //X坐标轴字体是斜的显示还是直的，true是斜的显示   
	    axisX.setTextColor(Color.parseColor("#373738"));//字体颜色
	    
	    axisX.setName("日期/天");  
	    axisX.setTextSize(11);
	    axisX.setMaxLabelChars(7); 
	    axisX.setValues(mAxisXValues);  
	    data.setAxisXBottom(axisX); 
//	    data.setAxisXTop(axisX);  
	    axisX.setHasLines(true); 
	    
	 // Y轴是根据数据的大小自动设置Y轴上限(在下面我会给出固定Y轴数据个数的解决方案
	    Axis axisY = new Axis();  
	    axisY.setName("金额（收入||支出）/元");//
	    axisY.setTextColor(Color.parseColor("#373738"));//字体颜色ע
	    axisY.setTextSize(11);//
	    data.setAxisYLeft(axisY);  //
      //data.setAxisYRight(axisY);  //

	    lineChart.setInteractive(true); 
//	    lineChart.setZoomType(ZoomType.HORIZONTAL);  
	    lineChart.setMaxZoom((float) 3);
	    lineChart.setLineChartData(data);  	    
	    lineChart.setVisibility(View.VISIBLE);
	    
	    Viewport v = new Viewport(lineChart.getMaximumViewport()); 
		  v.left = 0; 
		  v.right= 7; 
		  lineChart.setCurrentViewport(v);
    }
    
    /**
     * x
     */
    private void getAxisXLables(){
    	if(((year)/4 == 0) && month == 2){
    		for (int i = 0; i < date3.length; ++i) {    
            	mAxisXValues.add(new AxisValue(i).setLabel(date3[i]));  
            	Log.i(tag, "date3 = " + i);
            }
    	}else if((year/4 !=0) && month == 2){
    		for (int i = 0; i < date4.length; ++i) {    
            	mAxisXValues.add(new AxisValue(i).setLabel(date4[i]));  
            	Log.i(tag, "date4 = " + i);
            }
    	}else if((year/4 !=0) && (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)){
    		for (int i = 0; i < date1.length; ++i) {    
            	mAxisXValues.add(new AxisValue(i).setLabel(date1[i]));  
            	Log.i(tag, "date1 = " + i);
            }
    	}else{
    		for (int i = 0; i < date2.length; ++i) {    
            	mAxisXValues.add(new AxisValue(i).setLabel(date2[i]));    
            	Log.i(tag, "date2 = " + i);
            }
    	}
    }
    /**
     * y
     */
    private void getAxisPoints(){
    	Log.i(tag, "getAxisPoints()" );
    	if(((year)/4 == 0) && month == 2){
    		score = inputday(flag,29);
    	}else if((year/4 !=0) && month == 2){
    		score = inputday(flag,28);
    	}else if((year/4 !=0) && (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)){
    		score = inputday(flag,31);
    	}else{
    		score = inputday(flag,30);
    	}
        for (int i = 0; i < score.length; i++) {    
        	mPointValues.add(new PointValue(i, score[i]));    
        	Log.i(tag, "score = " + score[i]);
        }
        flag=1;
        if(((year)/4 == 0) && month == 2){
    		score = inputday(flag,29);
    	}else if((year/4 !=0) && month == 2){
    		score = inputday(flag,28);
    	}else if((year/4 !=0) && (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)){
    		score = inputday(flag,31);
    	}else{
    		score = inputday(flag,30);
    	}
        for (int i = 0; i < score.length; ++i) {    
        	mPointValues1.add(new PointValue(i, score[i]));    
//        	Log.i(tag, "score = " + score[i]);
        }
    }
    private int[] inputday(int flag,int days){
//    	Log.i(tag, "days = " + days);
    	int [] score = new int [days];
    	for(int i = 0; i < days; ++i){
    		int temp = dataHelper.findZhichuByYearAndMonthAndDay(flag,year,month,i);
    		if(temp>0)
    			score[i] = temp;
    		else
    			score[i] = 0;
    	}
    	return score;
    }
}
